package com.zzj.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzj.entity.Like;
import com.zzj.mapper.LikeMapper;
import com.zzj.service.ILikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-05-13
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements ILikeService {

    @Override
    public Like save(int targetId, int targetType, String fromUsername, String toUsername) {
        Like like = new Like();
        like.setTargetId(targetId);
        like.setFromUsername(fromUsername);
        like.setTargetType(targetType);
        like.setToUsername(toUsername);
        boolean save = this.save(like);
        if (save) return like;
        else return null;
    }

    @Override
    public boolean remove(int targetId, String fromUsername) {
        int targetType = 3;
        boolean remove = this.lambdaUpdate()
                .eq(Like::getTargetId, targetId)
                .eq(Like::getTargetType, targetType)
                .eq(Like::getFromUsername, fromUsername)
                .remove();
        return remove;
    }

    @Override
    public Like findLike(int targetId, String fromUsername) {
        return this.getOne(Wrappers.<Like>lambdaQuery()
                        .eq(Like::getTargetId, targetId)
                .eq(Like::getFromUsername, fromUsername),
                false);

    }
}
